<?php

/**
 * @author PeoneEr
 * @package aion personal cabinet v1.0
 * @copyright Aion Factory Inc. 2010-2011
 * @license GNU v3
 * @link Ai-Factory.ru && http://code.google.com/p/aionprivateofficev2
 */
require_once "core/application/models/mlogon/mlogon.php";
require_once "core/config.php";
require_once "core/application/classes/errorClass.php";

class auth_Controller extends Logon_Model {

    /**
     *
     * @var static $_instance
     * @var object $_lang
     * @var object $_conf
     * @var array $_params
     * @var array $_fetch
     */
    private static $_instance = NULL;
    public $_lang             = NULL;
    public $_conf             = NULL;
    private $_params       = array();
    private $_fetch        = array();
    public $_password         = NULL;

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    private function catchParams() {
        /**
         *  параметры из $_POST
         */
        $params = array(
            'login' => $_POST['login'],
            'password' => $_POST['password']
        );
        return $params;
    }

    public function enterToAccount() {
        /**
         *  Основная функция входа
         * 
         */
        $this->_lang = $this->getLanguage();
        $this->_conf = new Config();
        $this->_params = $this->catchParams();
        try {
            foreach ($this->_params as $key => $val) {
                if (empty($val)) {
                    $this->redirect(10, "index");
                    unset($key);
                    $this->echoError("Не все поля заполнены");
                }
            }
        } catch (Exception $e) {
            echo $e->getFile();
            echo $e->getLine();
            echo $e->getMessage();
        }
        $this->_params['password'] = $this->cryptPassword($this->_params['password']);
        if (!$this->issetAccount($this->_params['login'], $this->_params['password'])) {
            $this->echoError("Не верный пароль");
            $this->insertNewLog($this->_params['login'], "unsuccess", "enter");
        }
        /**
         * запрос в базу по данному юзверю
         */
        $this->_fetch = mysql_fetch_assoc($this->getInfoForLogin($this->_params['login'], $this->_params['password']));
        $this->_fetch['password'] = NULL;
        /**
         * переборка массива
         */
        try {
            foreach ($this->_fetch as $key => $val) {
                if ($key == 'password') {
                    $_SESSION['password'] = NULL;
                    continue;
                }
                $_SESSION[$key] = $val;
            }
            unset($this->_fetch);
        } catch (Exception $e) {
            echo $e->getLine();
            echo $e->getFile();
            echo $e->getMessage();
        }

        /**
         * язык
         */
        switch (valueSelect::selectValue(19)) {
            case 0: $_SESSION['language'] = "ru_RU";
                break;
            case 1: $_SESSION['language'] = "en_EN";
                break;
            default: $_SESSION['language'] = "ru_RU";
                break;
        }

        /**
         * проверка доступа в админку
         *  @param $_SESSION['access_level']
         *      если $_SESSION['access_level'] >= тому, что указано в конфиге - доступ в админку есть
         */
        if ($_SESSION['access_level'] >= $this->_conf->configData['access_level']) {
            $_SESSION['allow_adm'] = "allow";
        } else {
            unset($_SESSION['allow_adm']);
        }
        $this->insertNewLog($_SESSION['name'], "success", "enter");
        $this->redirect(0, "index");
        $_SESSION['justEnter'] = 1;
        unset($this->_params);
    }

    public function exitFromAccount() {
        /**
         *
         *  выход с аккаунта
         */
        if (empty($_SESSION['name']) || !isset($_SESSION['name']) || is_null($_SESSION['name'])) {
            $this->insertNewLog($_SESSION['name'], "unsuccess", "exit");
            $this->echoError("Вы не вошли");
        } else {
            $this->insertNewLog($_SESSION['name'], "success", "exit");
            echo "<script language='javascript'> $.jmessage('Успешный выход', 'Вы успешно вышли со своего аккаунта', 3000); showImg(); </script>";
            $this->redirect(2, "index");
            session_destroy();
            unset($_SESSION);
        }
    }

    public function AccountEqPassword($account, $password) {
        /**
         *  проверка логина на существование
         */
        $this->_password = $this->cryptPassword($password);
        if ($this->issetAccount($account, $this->_password)) {
            echo "<script language='javascript'>openEnterButton(); showOk(); </script>";
        } else {
            echo "<script language='javascript'>showError();</script>";
        }
    }

}

?>
